#include "stdafx.h"
#include "DataSeting.h"
#include "AppIniConfigAdapter.h"
#include "Util.h"
#include "PakFile.h"



CDataSeting &Data()
{
    static CDataSeting T;
    return T;
}

CDataSeting::CDataSeting()
{
    //ini data
    CAppIniConfigAdapter oIniAdapter;
    CString str = ModulePath()+c_InitSeting;
    oIniAdapter.Init(str.GetString());
    s_Seting[_T("DLL")] = oIniAdapter.GetString(_T("seting"), _T("DLL"));
    s_Seting[_T("EXE")] = oIniAdapter.GetString(_T("seting"), _T("EXE")); 
    s_Seting[_T("CONFIG")] = oIniAdapter.GetString(_T("seting"), _T("CONFIG")); 
    //UserInfo data
    {
        CUserInfo::ReadData(s_mapUserInfo);
    }
};

void CDataSeting::Init(LPCTSTR lpszName)
{
    s_Seting.clear();
    CAppIniConfigAdapter oIniAdapter;
    CString str = CUtil::GetModuleDirectory()+c_InitSeting;
    oIniAdapter.Init(str.GetString());
    s_Seting[_T("CONFIG")] = oIniAdapter.GetString(_T("seting"), _T("CONFIG")); 
    m_oAdapter.Init(lpszName);
    m_oAdapter.ReadToMap(s_Seting);
    s_Seting[_T("DLL")] = oIniAdapter.GetString(_T("seting"), _T("DLL"));
    s_Seting[_T("EXE")] = oIniAdapter.GetString(_T("seting"), _T("EXE")); 
}

void CDataSeting::Create(LPCTSTR lpszName)
{
    s_Seting.clear();
    CAppIniConfigAdapter oIniAdapter;
    CString str = CUtil::GetModuleDirectory()+c_InitSeting;
    oIniAdapter.Init(str.GetString());

    s_Seting[_T("CONFIG")] = oIniAdapter.GetString(_T("seting"), _T("CONFIG")); 

    ::CopyFile(CUtil::GetModuleDirectory()+c_DataSeting+_T("stand.xml"), lpszName, 0);
    m_oAdapter.Init(lpszName);
    m_oAdapter.ReadToMap(s_Seting);
    s_Seting[_T("DLL")] = oIniAdapter.GetString(_T("seting"), _T("DLL"));
    s_Seting[_T("EXE")] = oIniAdapter.GetString(_T("seting"), _T("EXE")); 
}

void CDataSeting::AddItem(HTREEITEM hItem, CString &strData)
{
    sEdit oo;
    oo.Init(strData);
    s_Data[hItem] = oo;
}

sEdit CDataSeting::GetItem(HTREEITEM hItem)
{
    static sEdit o;
    std::map<HTREEITEM, sEdit>::iterator itor;
    itor = s_Data.find(hItem);
    if (itor==s_Data.end()) return o;
    return (*itor).second;
}

CString CDataSeting::GetData(CString &strKey)
{
    return s_Seting[strKey];
}

CString CDataSeting::GetData(LPTSTR strKey)
{
    return s_Seting[strKey];
}

CString CDataSeting::GetSpellCast(CString &strKey)
{
    return m_oSpellCast.GetData(strKey);
}

void CDataSeting::SetData(CString &strKey, CString &strData)
{
    s_Seting[strKey] = strData;
    SaveData();
}

void CDataSeting::SetData(CString &strKey, LPTSTR strData)
{
    s_Seting[strKey] = strData;
    SaveData();
}

void CDataSeting::SaveData()
{
    m_oAdapter.WriteToLacol(s_Seting);
}

void CDataSeting::SetIni(const CString &strKey, CString &strData)
{
    CAppIniConfigAdapter oIniAdapter;
    CString str = CUtil::GetModuleDirectory()+c_InitSeting;
    oIniAdapter.Init(str.GetString());
    oIniAdapter.WriteString(_T("seting"), strKey, strData);
}

CUserInfo CDataSeting::GetUser(int index)
{
    std::map<int, CUserInfo>::iterator itr = s_mapUserInfo.find(index);
    if (itr==s_mapUserInfo.end())
        return CUserInfo();
    return itr->second;
}

void CDataSeting::AddUser(int index, CUserInfo& user)
{
    std::map<int, CUserInfo>::iterator itr = s_mapUserInfo.begin();
    for (; itr!=s_mapUserInfo.end(); ++itr)
    {
        if (itr->second.User()==user.User())
            return;
    }
    s_mapUserInfo[(int)s_mapUserInfo.size()] = user;
    CUserInfo::WriteData(s_mapUserInfo);
}

bool CUserInfo::ReadData(std::map<int, CUserInfo>& vecData)
{
    HANDLE hFile;
    vecData.clear();
    if (!PAK().OpenFile(c_UserInfo, &hFile)) return false;

    DWORD dwSize = PAK().GetFileSize(hFile);
    if (dwSize%sizeof(CUserInfo))
    {
       PAK().CloseFile(hFile);
       return false;
    }

    BYTE *_Buf = new BYTE[dwSize];
    PAK().ReadFile(hFile, _Buf, dwSize);
    PAK().CloseFile(hFile);
    int index = 0;
    for (DWORD dwPos=0; dwPos<dwSize; dwPos+=sizeof(CUserInfo))
    {
        CUserInfo oo;
        oo.Init(_Buf+dwPos);
        vecData[index]=oo;
        ++index;
    }
    
    delete [] _Buf;
    return true;
}
bool CUserInfo::WriteData(std::map<int, CUserInfo>& vecData)
{
    if (vecData.size()==0) return false;
    int dwSize = (int)vecData.size();
    BYTE*_Buf = new BYTE[dwSize*sizeof(CUserInfo)];

    for (int dwIndex=0; dwIndex<dwSize; ++dwIndex)
    {
        memcpy(_Buf + dwIndex * sizeof(CUserInfo), vecData[dwIndex].DataPtr(), sizeof(CUserInfo));
    }

    PAK().WriteBuffFile((const char*)_Buf, (DWORD)vecData.size()*sizeof(CUserInfo), c_UserInfo, 0);
    delete [] _Buf;
    return true;
}

void CIPDatas::Init()
{
    //c_IPFormat
    //c_IPInfo
    HANDLE hFile;
    char   _Buf[4096];
    m_Data.clear();
    m_Key.clear();

    {
        if (!PAK().OpenFile(c_IPFormat, &hFile)) return;

        DWORD dwSize = PAK().GetFileSize(hFile);
        PAK().ReadFile(hFile, _Buf, dwSize);
        PAK().CloseFile(hFile);
        _Buf[dwSize] = 0;

        int iStart = 0;
        CString str(_Buf);
        CString strItem;
        while(1)
        {
            strItem = str.Tokenize(_T("\r\n"), iStart);
            if (iStart==-1) break;
            int i = strItem.Find(_T(":"));

            if (strItem.Left(i)==_T("Format"))
            {
                m_strFormat = strItem.Mid(i+1);
            }
            else if (strItem.Left(i)==_T("Key"))
            {
                m_Key.push_back(strItem.Mid(i+1));
            }
            else if (strItem.Left(i)==_T("Format1"))
            {
                m_strFormat1 = strItem.Mid(i+1);
            }
        }
    }

    {
        if (!PAK().OpenFile(c_IPInfo, &hFile)) return;

        DWORD dwSize = PAK().GetFileSize(hFile);
        PAK().ReadFile(hFile, _Buf, dwSize);
        PAK().CloseFile(hFile);
        _Buf[dwSize] = 0;

        int iStart = 0;
        CString str(_Buf);
        CString strItem;
        while(1)
        {
            strItem = str.Tokenize(_T("\r\n"), iStart);
            if (iStart==-1) break;
            
            if (strItem.Find(_T("IP="))==0)
            {
                strItem.Replace(_T("IP="), _T(""));
                m_Data.push_back(strItem);
            }
            else if (strItem.Find(_T("HOME="))==0)
            {
                strItem.Replace(_T("HOME="), _T(""));
                m_strHome = strItem;
            }
            else if (strItem.Find(_T("REG="))==0)
            {
                strItem.Replace(_T("REG="), _T(""));
                m_strRegister = strItem;
            }
            else if (strItem.Find(_T("ID="))==0)
            {
                strItem.Replace(_T("ID="), _T(""));
                m_strID = strItem;
            }
        }

    }
}